﻿
using System ;
using System.Drawing ;
using System.Windows.Forms ;

using MskEdit.Msk.Model ;


namespace MskEdit.Msk.Frontend {

    static class MskCellGeometryFactory {  

        private static Size getLabelSizeOfText( Form form, Label label, string text ) {
        
            bool   originalAutoSize = label.AutoSize ;            
            string originalText     = label.Text ;
            
            form.Controls.Add( label ) ;
            label.AutoSize = true ;
            label.Text     = text ;
            Size labelSize = label.Size ;
            form.Controls.Remove( label ) ;
            
            label.Text     = originalText ;
            label.AutoSize = originalAutoSize ;
            
            return labelSize ;
        }
    
    
        public static MskCellGeometry createFrom( Form form, Label label ) {
            
            Size size1  = getLabelSizeOfText( form, label, "x" ) ;
            Size size10 = getLabelSizeOfText( form, label, "1234567890" ) ;            
      
            int cellWidth    = ( int ) ( ( size10.Width - size1.Width ) / 9.0f ) ;            
            int cellHeight   = Math.Max( size1.Height, size10.Height ) ; // Should be the same anyway
            int labelPadding = ( size1.Width - cellWidth ) / 2 ;
            
            Size cellSize    = new Size( cellWidth, cellHeight ) ;

            MskCellGeometry cellGeometry = new MskCellGeometry( cellSize, labelPadding ) ;
            
            return cellGeometry ;
        }
    }
}

